<?php

namespace app\user\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Login extends Controller
{
    //

    public function index()
    {
        return view('user/login');
    }

    public function login(Request $request)
    {
        $post = $request->post();
//        var_dump($post);
        $res=Db::name('user')->field(['pwd'],true)->where('tel',$post['username'])->where('pwd',md5($post['password']))->find();
//        var_dump($res);


        if($res){
            if($res['status']!=1){
                return "<script>alert('该用户已被禁用');history.go(-1);</script>";
            }

            Session::set('user',$res);
//            var_dump(Request::instance()->session());die;


            $this->redirect('user/Index/index');
        }else{
            return "<script>alert('账号或密码错误');history.go(-1);</script>";
        }




    }

    public function register()
    {
        return view('user/register');
    }

    public function telreg(Request $request)
    {
        $post=$request->post();
//        var_dump($post);die;
//die;
        $telcode=Session::get('temp.telcode');
        if(!$telcode){
            return "<script>alert('请先获取验证码');history.go(-1);</script>";
        }
        if($telcode!=$post['mobile_code']){
            return "<script>alert('验证码错误');history.go(-1);</script>";
        }
        if($post['password']!=$post['repassword']){
            return "<script>alert('两次密码不同');history.go(-1);</script>";
        }

        $temp['tel']=$post['tel'];
        $temp['pwd']=md5($post['password']);



        $id=Db::name('user')->insertGetId($temp);
//        var_dump($res);die;
        $res=Db::name('user_data')->insert(['uid'=>$id]);

        if($res){

            $mid=Db::name('msg')->insertGetId(['msg'=>'欢迎来到期权交易平台','time'=>time()]);
            $res=Db::name('user_msg')->insert(['mid'=>$mid,'uid'=>$id,'status'=>'2']);
        }else{
//            $this->error('注册失败','user/Login/register');
            return "<script>alert('注册失败,请稍后重试');history.go(-1);</script>";
        }


        if ($res){
//            $this->success('注册成功,现在去登录','user/Login/index');
            return "<script>alert('注册成功,现在去登录');window.location.href='".url('user/Login/index')."'</script>";
        }else{
//            $this->error('注册失败','user/Login/register');
            return "<script>alert('注册失败,请稍后重试');history.go(-1);</script>";
        }

    }

    public function exitlogin()
    {
        Session::clear();
//        $session=Request::instance()->session();
//        var_dump($session);
//        die;
        $this->redirect('user/Login/index');
    }

    public function reloginpwd()
    {
//        return 111;
        return view('user/reloginpwd');
    }

    public function doreloginpwd(Request $request)
    {
//        return 111;
        $post=$request->post();
//        var_dump($post);
        Session::set('temp.tel',$post['newmobile']);
        $telcode=Session::get('temp.telcode');

        if($telcode!=$post['code']){
            return "<script>alert('验证码错误');history.go(-1);</script>";
        }
        return view('user/reloginpwd1');
    }

    public function doreloginpwd1(Request $request)
    {
        $post=$request->post();
        $tel=Session::get('temp.tel');
//        var_dump($post);
//        var_dump($tel);
        $res=Db::name('user')->where('tel',$tel)->update(['pwd'=>md5($post['newpwd'])]);
        if($res){
            return "<script>alert('重置密码成功,现在去登录');window.location.href='".url('user/Login/index')."'</script>";
        }else{
            return "<script>alert('新密码与原密码相同');history.go(-1);</script>";
        }
    }

    public function guize()
    {
        return view('user/guize');
    }

    public function tiaokuan()
    {
        return view('user/tiaokuan');
    }


}
